(function ($)
{
    $.suggesting = {};

    $.suggesting.template = {};

    $.suggesting.template.index = {};
    $.suggesting.template.new_element = {};
    $.suggesting.template.new_combination = {};
    $.suggesting.template.new_picture = {};

    $.suggesting.initialized = false

    $.suggesting.init = function (params)
    {
        if ($.suggesting.initialized)
        {
            $('#suggesting-index').verticalShow();

            $('#suggesting-index-nav').verticalHide();

            return;
        }

        $.suggesting.initialized = true;

        var keys = Object.keys($.suggesting.template);

        for (var i = 0; i < keys.length; i++)
        {
            $.suggesting.loadHtml(keys[i], keys.length);
        }
    }

    $.suggesting.loadedMembers = 1;

    $.suggesting.loadHtml = function (name, membersCount)
    {
        $.post('suggesting-' + name.toString().replace('_', '-') + '.html', null, function (data)
        {
            $.suggesting.loadedMembers++;

            $.suggesting.template[name].html = data;

            if (membersCount == $.suggesting.loadedMembers)
                $.suggesting.template_onLoad();
        }, 'html');
    }

    $.suggesting.template_onLoad = function ()
    {
        $('#suggesting').html($.suggesting.template.index.html);

        $('.button').button();

        $('#suggesting-index-back').click($.suggesting.back_onClick)

        $('#suggesting-index a').click($.suggesting.button_onClick);
    }

    $.suggesting.button_onClick = function (event, ui)
    {
        var hash = event.currentTarget.hash.replace('#', '').replace('-', '_');

        $('#suggesting-index-content').html($.suggesting.template[hash].html);

        $('#suggesting-index').verticalHide();

        $('#suggesting-index-nav').verticalShow();

        event.preventDefault();
    }

    $.suggesting.back_onClick = function ()
    {
        $.suggesting.init();
    }

})(jQuery);
